package com.situ.struts.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.situ.struts.entity.User;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

public class UserAction extends ActionSupport implements ModelDriven<User> {
    private User user = new User();

    public String login() {
        System.out.println("UserAction.login");
        // 调用Service->Dao验证
//        boolean isSuccess = userService.login(user);
        if ("zhangsan".equals(user.getName()) && "123".equals(user.getPassword())) {
            // 登录成功，放到Session里面
            HttpServletRequest request = ServletActionContext.getRequest();
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            return "loginSuccess";
        } else {
            return "loginFail";
        }
    }

    public String list() {
        System.out.println("UserAction.list");
        User user1 = new User(1, "zhangsan1", "123");
        User user2 = new User(2, "zhangsan2", "123");
        User user3 = new User(3, "zhangsan3", "123");
        List<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        HttpServletRequest request = ServletActionContext.getRequest();
        request.setAttribute("list", list);
        return "list";
    }

    @Override
    public User getModel() {
        return user;
    }
}
